<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDepart('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形结构 -->
        <el-tree :data="list" @node-click="handleNodeClick">
          <template v-slot="{data}">
            <!-- {{ data }} -->
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">

                  <el-col>{{ data.manager }}</el-col>

                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepart(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDepart(data.id)">编辑子部门</el-dropdown-item>
                        <el-dropdown-item v-if="!data.children.length" @click.native="delDepartment(data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isedit ? '编辑内容' : '添加内容'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @closed="hclose"
      >
        <dept-dialog

          :id="depId"
          ref="diaref"
          :is-edit="isedit"
          :origin-list="origin"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getListTree } from '@/utils/index'
import { getDepartment, delDepartment } from '@/api/department'
import deptDialog from './deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      depId: '',
      isedit: false,
      origin: []
    }
  },
  created() {
    this.getdepartment()
  },
  methods: {
    hclose() {
      this.$refs.diaref.resetform()
    },
    delDepartment(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await delDepartment(id)
          this.getdepartment()
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {

      })
    },
    editDepart(id) {
      this.showDialog = true
      this.depId = id
      this.isedit = true
      // DOM是异步的，获取的数据还是上一次的，DOM元素更新之后调用下面的语句
      this.$nextTick(() => {
        this.$refs.diaref.loadDepartmentdet()
      })
    },
    hSuccess() {
      this.showDialog = false
      this.getdepartment()
    },
    addDepart(id) {
      this.showDialog = true
      console.log(id)
      this.depId = id
      this.isedit = false
    },
    handleNodeClick(data) {
      console.log(data)
    },
    async  getdepartment() {
      const res = await getDepartment()
      // console.log(res)
      this.origin = res.data.depts.map((item) => {
        const { pid, id, code, name } = item
        return { pid, id, code, name }
      })
      const company = res.data.depts.shift()
      console.log(company)
      // console.log(res.data.depts)
      this.list = getListTree(res.data.depts)
      // this.list = res.data.depts
    }
  }
}
</script>
